package org.liaohailong.victorlibrary.ui

import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import org.liaohailong.library.util.JsonUtil
import org.liaohailong.library.util.Utility
import org.liaohailong.library.widget.PerformanceAdapter
import org.liaohailong.library.widget.section.SectionListDelegate
import org.liaohailong.victorlibrary.R
import org.liaohailong.victorlibrary.base.BaseActivity
import org.liaohailong.victorlibrary.model.City
import org.liaohailong.victorlibrary.model.Province
import java.util.concurrent.Executors

/**
 * Author: liaohailong
 * Date: 2019/4/27
 * Time: 10:24
 * Description: 分类列表+顶部吸附
 **/
class SectionListActivity : BaseActivity() {
    companion object {
        fun show(context: Context) {
            val intent = Intent(context, SectionListActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val cityList: MutableList<City> = mutableListOf()

    private val adapter = Adapter()
    private val headerCount: Int = 0
    private val onTitleChangedListener: SectionListDelegate.OnTitleChangedListener = object : SectionListDelegate.OnTitleChangedListener {
        override fun inDataPos(position: Int): Boolean {
            return position >= headerCount
        }

        override fun hasTitle(position: Int): Boolean {
            val pos = position - headerCount
            if (pos < 0) {
                return false
            }
            if (pos == 0) {
                return true
            }

            val data = adapter.data[position]
            val lastData = adapter.data[position - 1]
            return !TextUtils.equals(data.provinceName, lastData.provinceName)
        }

        override fun onTitleChanged(titleView: View?, adapter: RecyclerView.Adapter<*>?, position: Int) {
            titleView?.apply {
                val titleTxt = findViewById<TextView>(R.id.title_txt)
                titleTxt.text = this@SectionListActivity.adapter.data[position - headerCount].provinceName
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_section_list)

        val sectionListView = findViewById<RecyclerView>(R.id.section_list_view)
        val sectionHeaderView = findViewById<FrameLayout>(R.id.section_title_view)
        val sectionListDelegate = SectionListDelegate()

        sectionListDelegate.attachRecyclerView(sectionListView)
        sectionListDelegate.setTitleView(sectionHeaderView)
        sectionListDelegate.setAdapter(adapter)
        sectionListDelegate.setOnTitleChangedListener(onTitleChangedListener)


        val progressDialog = ProgressDialog(this)
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER)
        progressDialog.setTitle("数据加载中...")
        progressDialog.show()

        //数据解析
        Executors.newSingleThreadExecutor().execute {
            val jsonString = Utility.getAssestFileAsString(this, "geoinfo-all.json")
            jsonString?.apply {
                cityList.clear()
                val data = JsonUtil.stringToList(jsonString, Province::class.java)
                for (province in data) {
                    for (city in province.districts) {
                        city.provinceName = province.name
                        cityList.add(city)
                    }
                }
            }
            runOnUiThread {
                progressDialog.hide()
                adapter.setData(cityList)
            }
        }
    }

    inner class Adapter : PerformanceAdapter<ViewHolder>() {
        val data: MutableList<City> = mutableListOf()

        fun setData(dataList: MutableList<City>) {
            data.clear()
            data.addAll(dataList)
            notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_section_list_item, parent, false)
            return ViewHolder(view)
        }

        override fun getItemCount(): Int {
            return data.size
        }

        override fun onBindViewHolder(holder: ViewHolder?, position: Int, performance: Boolean, payloads: MutableList<Any>?) {
            holder?.bindView(data[position], position)
        }
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val contentFormat = "%s : %s"

        fun bindView(city: City, position: Int) {
            //标题头部
            val titleTxt = itemView.findViewById<TextView>(R.id.title_txt)
            //内容部分
            val contentTxt = itemView.findViewById<TextView>(R.id.content_txt)

            titleTxt.text = city.provinceName
            contentTxt.text = String.format(contentFormat, city.name, city.center)

            val hasTitle = onTitleChangedListener.hasTitle(position)
            val headerView = itemView.findViewById<FrameLayout>(R.id.section_item_header_view)
            headerView.visibility = if (hasTitle) View.VISIBLE else View.GONE
        }
    }
}